# import python modules
import sys, os.path, random, time, math

# import basic pygame modules
import pygame
from pygame.locals import *

# import side modules
from PlayerClass import *
from AimClass import *
from LineOfSightClass import *
from BackgroundClass import *
from ImageHandler import *
from SoundHandler import *
from KeyboardHandler import *
from CheckFPS import *

# check if more than the standard image formats are acceptable
if not pygame.image.get_extended():
    raise SystemExit('Image could not be loaded')

# game constants
SCREEN_SIZE = (640, 480)
SCREEN_POSITION = (0, 0)
WINDOW_STYLE = 0 # can be changed to FULLSCREEN
GAME_NAME = 'Zombie Shooter'
FRAMES_PER_SECOND = 60

# main game loop
def main():
    # initialize pygame
    pygame.init()
    if pygame.mixer and not pygame.mixer.get_init():
        print('Warning, no sound')
        pygame.mixer = None

    # set game window
    screen = pygame.display.set_mode(SCREEN_SIZE, WINDOW_STYLE, 32)
    pygame.display.set_caption(GAME_NAME)

    # load images
    images = ImageHandler()
    Player.images.append(images.load_image('Splayer.png'))
    Aim.images.append(images.load_image('Sdot.png'))
    Background.images.append(images.load_image('Sbackground.png'))

    # load sound
    sounds = SoundHandler()
    if pygame.mixer:
        main_music = sounds.load_sound('backmusic.ogg')
        main_music.play(-1)

    # initialize Game Groups
    all = pygame.sprite.RenderUpdates()

    # assign default groups to each sprite class
    Player.containers = all
    Aim.containers = all
    Check_FPS.containers = all

    # create some starting values
    clock = pygame.time.Clock()

    # setting mouse to invisible
    pygame.mouse.set_visible(False)

    # initialize starting objects and sprites
    player = Player(200, 200)
    aim = Aim()
    keyboard = KeyboardHandler()
    if pygame.font:
        fps = Check_FPS()
    
    # initializing background
    background = Background()
    screen.blit(background.image, (0, 0))
    pygame.display.flip()

    # initializing line of sight
    sight = Sight(screen, background.image)

    # main loop
    while True:

        # look for input
        if keyboard.quit == True:
            pygame.quit()
            sys.exit()
        keyboard.check_input()

        # clear/erase the last drawn sprites
        all.clear(screen, background.image)
        sight.clear()

        # update all sprites
        all.update()

        # updating frames per second text
        fps.set_fps(int(clock.get_fps()))

        # handle player input
        player.check_mouse_input(pygame.mouse.get_pos())
        player.move(keyboard)

        # updating aim
        aim.check(player.mouse_position)
        
        # draw the scene
        dirty = all.draw(screen)
        pygame.display.update(dirty)
        pygame.display.update(sight.check_and_draw(player.rect.center, player.mouse_position))
   
        # frame rate        
        clock.tick(FRAMES_PER_SECOND)

# call the main function if running this scrip
if __name__ == '__main__': main()   
